package com.qiyadeng.wechat.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.TreeMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.qiyadeng.wechat.baidu.BaiduPlaceResponse;
import com.qiyadeng.wechat.baidu.GeoUtil;
import com.qiyadeng.wechat.baidu.GeoUtil.GaussSphere;
import com.qiyadeng.wechat.config.WeChatConstant;
import com.qiyadeng.wechat.roo.model.ProWechat;
import com.qiyadeng.wechat.roo.model.Store;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class WeChatReplyNewsMessage {

	//private static Logger logger = Logger. getLogger("WeChatReplyNewsMessage");
	private static Logger logger = LoggerFactory.getLogger(WeChatReplyNewsMessage.class);
	private String toUserName;
	private String fromUserName;
	private String createTime;
	private String msgType;
	private int articleCount;
	private List<Item> items;
	private String funcFlag;
	
	//活动信息、产品信息查询
	public static String getProWeChat(List<ProWechat> proList, String userName, int size){
		WeChatReplyNewsMessage newsMessage = new WeChatReplyNewsMessage();
		List<Item> items = new ArrayList<Item>();
		StringBuffer strBuf = new StringBuffer();
		newsMessage.setItems(items);

		if(proList.size()>size){
			newsMessage.setArticleCount(size);
		}
		else{
			newsMessage.setArticleCount(proList.size());
		}
		newsMessage.setCreateTime(new Date().getTime()+"");
		newsMessage.setMsgType("news");
		newsMessage.setFuncFlag("0");
		newsMessage.setToUserName(userName);
		newsMessage.setFromUserName(WeChatConstant.FROMUSERNAME);
		for(int i = 0;i <newsMessage.getArticleCount();i++){
			ProWechat pro = proList.get(i);
				Item item = new Item();
				item.setTitle(pro.getProName());
				item.setPicUrl(pro.getProPic());
				if(pro.getUrl()!=null && pro.getUrl().length()>0 ){
					item.setUrl(pro.getUrl());
				}
				else {
					item.setUrl(WeChatConstant.BASE_PIC_WEBSITE+"/wechat/mobilepro/?proId="+pro.getId());
				}
				
				item.setDescription(pro.getProDes());
				items.add(item);
		}
		strBuf = strBuf.append(getWeChatNewsMessage(newsMessage));
		return strBuf.toString();
	}
	
	//门店查询 
	//TODO  返回结果数量 等待调试
	public static String getSotreWeChatReplyNewsMessage(TreeMap<Double, Integer> treeMap ,List<Store> storeList, double lat, double lng, String userName, int size){
		WeChatReplyNewsMessage newsMessage = new WeChatReplyNewsMessage();
		List<Item> items = new ArrayList<Item>();
		StringBuffer strBuf = new StringBuffer();
		newsMessage.setItems(items);
		newsMessage.setCreateTime(new Date().getTime()+"");
		newsMessage.setMsgType("news");
		newsMessage.setFuncFlag("0");
		newsMessage.setToUserName(userName);
		newsMessage.setFromUserName(WeChatConstant.FROMUSERNAME);
		
		Set<Double> set = treeMap.keySet();
		for (Double double1 : set) {
			if(items.size()<size){
				int index = treeMap.get(double1);
				Store store = storeList.get(index);
				Item item = new Item();
				//item.setTitle(store.getStoreName()+"["+double1+"米]"+"\n"+store.getStoreAddress()+"\n"+store.getStoreTel());
				item.setTitle(store.getStoreName()+"["+double1+"米]"+"\n");
				item.setPicUrl(WeChatConstant.BASE_PIC_WEBSITE+store.getStorePic());
				item.setUrl(WeChatConstant.BASE_PIC_WEBSITE+"/wechat/mobilestore/?storeId="+store.getId());
				item.setDescription(store.getStoreName()+"["+double1+"米]"+"\n"+store.getStoreAddress()+"\n"+store.getStoreTel());
				items.add(item);
			}else {
				break;
			}
			
		}
		if(items.size()<size){
			newsMessage.setArticleCount(items.size());
		}
		else {
			newsMessage.setArticleCount(size);
		}
		
		strBuf = strBuf.append(getWeChatNewsMessage(newsMessage));
		return strBuf.toString();
	}
	
	public static String getWeChatReplyNewsMessageByBaiduPlace(List<BaiduPlaceResponse> placeList, double lat, double lng,String userName, int size){
		WeChatReplyNewsMessage newsMessage = new WeChatReplyNewsMessage();
		List<Item> items = new ArrayList<Item>();
		StringBuffer strBuf = new StringBuffer();
		logger.debug("placeList count="+placeList.size());
		newsMessage.setItems(items);
		if(placeList.size()>size){
			newsMessage.setArticleCount(size);
		}
		else{
			newsMessage.setArticleCount(placeList.size());
		}
		logger.debug("article count="+newsMessage.getArticleCount());
		newsMessage.setCreateTime(new Date().getTime()+"");
		newsMessage.setMsgType("news");
		newsMessage.setFuncFlag("0");
		newsMessage.setToUserName(userName);
		newsMessage.setFromUserName(WeChatConstant.FROMUSERNAME);
		for(int i = 0;i <newsMessage.getArticleCount();i++){
			BaiduPlaceResponse place = placeList.get(i);
			Double distance = GeoUtil.DistanceOfTwoPoints(Double.valueOf(place.getLng()), Double.valueOf(place.getLat()), lng, lat, GaussSphere.Beijing54);
			Item item = new Item();
			item.setTitle(place.getName()+"["+distance+"米]"+"\n"+place.getAddress()+"\n"+place.getTelephone());
			item.setPicUrl("");
			item.setUrl(place.getDetailUrl());
			item.setDescription("");
			items.add(item);
		}
		logger.debug("newMessage="+newsMessage.toString());
		strBuf = strBuf.append(getWeChatNewsMessage(newsMessage));
		
		return strBuf.toString();
	}
	
	public static String getWeChatNewsMessage(WeChatReplyNewsMessage newsMessage){
		XStream xstream = new XStream(new DomDriver());
		xstream.alias("xml", WeChatReplyNewsMessage.class);
		xstream.aliasField("ToUserName", WeChatReplyNewsMessage.class, "toUserName");
		xstream.aliasField("FromUserName", WeChatReplyNewsMessage.class, "fromUserName");
		xstream.aliasField("CreateTime", WeChatReplyNewsMessage.class, "createTime");
		xstream.aliasField("MsgType", WeChatReplyNewsMessage.class, "msgType");
		xstream.aliasField("ArticleCount", WeChatReplyNewsMessage.class, "articleCount");
		xstream.aliasField("Content", WeChatReplyNewsMessage.class, "content");
		xstream.aliasField("FuncFlag", WeChatReplyNewsMessage.class, "funcFlag");
		xstream.aliasField("Articles", WeChatReplyNewsMessage.class, "items");
		
		xstream.alias("item", Item.class);
		xstream.aliasField("Title", Item.class, "title");
		xstream.aliasField("Description", Item.class, "description");
		xstream.aliasField("PicUrl", Item.class, "picUrl");
		xstream.aliasField("Url", Item.class, "url");
		
		return xstream.toXML(newsMessage);
	}
	
	
	public String getToUserName() {
		return toUserName;
	}
	public void setToUserName(String toUserName) {
		this.toUserName = toUserName;
	}
	public String getFromUserName() {
		return fromUserName;
	}
	public void setFromUserName(String fromUserName) {
		this.fromUserName = fromUserName;
	}
	public String getCreateTime() {
		return createTime;
	}
	public void setCreateTime(String createTime) {
		this.createTime = createTime;
	}
	public String getMsgType() {
		return msgType;
	}
	public void setMsgType(String msgType) {
		this.msgType = msgType;
	}
	public int getArticleCount() {
		return articleCount;
	}
	public void setArticleCount(int articleCount) {
		this.articleCount = articleCount;
	}
	public List<Item> getItems() {
		return items;
	}
	public void setItems(List<Item> items) {
		this.items = items;
	}
	public String getFuncFlag() {
		return funcFlag;
	}
	public void setFuncFlag(String funcFlag) {
		this.funcFlag = funcFlag;
	}

	@Override
	public String toString() {
		return "WeChatReplyNewsMessage [toUserName=" + toUserName
				+ ", fromUserName=" + fromUserName + ", createTime="
				+ createTime + ", msgType=" + msgType + ", articleCount="
				+ articleCount + ", items=" + items + ", funcFlag=" + funcFlag
				+ "]";
	}
}

class Item{
	private String title;
	private String description;
	private String picUrl;
	private String url;
	
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public String getPicUrl() {
		return picUrl;
	}
	public void setPicUrl(String picUrl) {
		this.picUrl = picUrl;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
}

